package com.jumi.microservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.domain.*;
import com.jumi.microservice.mapper.GoodsStockLogMapper;
import com.jumi.microservice.mapper.GoodsStockMapper;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * @author Administrator
 */
@DubboService
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private GoodsStockMapper goodsStockMapper;
    @Autowired
    private GoodsStockLogMapper goodsStockLogMapper;
    @Autowired
    private GoodsWarehouseStockMapper goodsWarehouseStockMapper;

    @Override
    public GoodsStockLogDTO paymentService(GoodsStockQuery goodsStockQuery) {
        //查询商品sku库存流水表
        GoodsStockLogDO goodsStockLogDO = goodsStockLogMapper.selectById(goodsStockQuery.getId());
        //查询商品sku库存表
        QueryWrapper<GoodsStockDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_sku_id", goodsStockQuery.getGoodsSkuId());
        GoodsStockDO goodsStockDO = goodsStockMapper.selectOne(queryWrapper);
        //判断是否是预售库存:(inventory_type:1真实库存，2.预售库存)
        if (goodsStockLogDO.getInventoryType() == 1 && goodsStockQuery.getWarehouseId() != 0) {
            //商品sku分仓库存表
            QueryWrapper<GoodsWarehouseStockDO> queryWrappers = new QueryWrapper<>();
            queryWrappers.eq("goods_sku_id", goodsStockQuery.getGoodsSkuId());
            queryWrappers.eq("warehouse_id", goodsStockQuery.getWarehouseId());
            GoodsWarehouseStockDO goodsWarehouseStockDO = goodsWarehouseStockMapper.selectOne(queryWrappers);
            //商品sku分仓库存表,减锁定库存增加已售库存
            //减锁定库存
            goodsWarehouseStockDO.setLockedStockQuantity(goodsWarehouseStockDO.getLockedStockQuantity() - goodsStockQuery.getLockedStockQuantity());
            //增加已销售库存
            goodsWarehouseStockDO.setSaledStockQuantity(goodsWarehouseStockDO.getSaledStockQuantity() + goodsStockQuery.getSaledStockQuantity());
            goodsWarehouseStockMapper.updateById(goodsWarehouseStockDO);

        }
        //根据流水号更新订单号，同时锁定库存清零
        //更新订单号
        goodsStockLogDO.setOrderSn(goodsStockQuery.getOrderSn());
        //锁定库存清0
        goodsStockLogDO.setLockedStorage(0);
        goodsStockLogDO.setUpdateTime(LocalDateTime.now());
        goodsStockLogMapper.updateById(goodsStockLogDO);
        //减锁定库存增加已售库存
        //减锁定库存
        goodsStockDO.setLockedStorage(goodsStockDO.getLockedStorage() - goodsStockQuery.getLockedStockQuantity());
        //增加已销售库存
        goodsStockDO.setSaledStorage(goodsStockDO.getSaledStorage() + goodsStockQuery.getSaledStockQuantity());
        goodsStockMapper.updateById(goodsStockDO);

        //返回
        GoodsStockLogDTO dto = new GoodsStockLogDTO();
        dto.setId(goodsStockLogDO.getId());
        dto.setOrderSn(goodsStockLogDO.getOrderSn());
        dto.setInventoryType(goodsStockLogDO.getInventoryType());
        return dto;
    }
}
